package com.hwl.lab.controller;

import com.hwl.lab.entity.Area;
import com.hwl.lab.entity.Role;
import com.hwl.lab.entity.RoleAndMenu;
import com.hwl.lab.repository.RoleAndMenuRepository;
import com.hwl.lab.service.AreaService;
import com.hwl.lab.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin //允许跨域请求
@RestController
public class AreaController {

    @Autowired
    private AreaService areaService;
    //查询所有（分页）
    @GetMapping("/findAreaAll/{page}/{size}")
    public Page<Area> findAreaAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        return areaService.findAll(page,size);
    }
    //查询所有不分页
    @GetMapping("/findAreaAll")
    public List<Area> findAll(){
        return areaService.findAll();
    }

    @GetMapping("/findByAreaidF/{areaid}")
    public List<Area> findByUserid(@PathVariable("areaid") String areaid){
        if (areaService.findByAreaid(areaid) != null) {
            List<Area> areas = new ArrayList<>();
            areas.add(areaService.findByAreaid(areaid));
            return areas;
        } else {
            return null;
        }
    }
    //添加，修改
    @PostMapping("/saveArea")
    public void saveArea(@RequestBody(required=false) Area area){
        areaService.saveArea(area);
    }

    //删除
    @PostMapping("/deleteArea/{id}")
    public void deleteArea(@PathVariable("id") Long id){
        areaService.deleteById(id);
    }
}
